<?php

namespace App\Jobs;

use App\Models\Posts\WxPost;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Redis;

class PostTypeUpdateJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
        $todo_set = Redis::smembers('post:type:to:update:set');
        if($todo_set){
            foreach ($todo_set as $str){
                if(_empty_($str)){
                    Redis::srem('post:type:to:update:set', $str);
                    continue;
                }
                $data = json_decode($str, true);
                if(_empty_($data)){
                    Redis::srem('post:type:to:update:set', $str);
                    continue;
                }
                $post_id = $data['id'];
                $type = $data['type'];

                WxPost::withoutSyncingToSearch(function () use ($post_id, $type){
                    WxPost::where('id', $post_id)->update([
                        'post_type' =>  $type
                    ]);
                });

                Redis::srem('post:type:to:update:set', $str);
            }
        }else{
//            _logger_(__file__, __line__, 'post:type:to:update:set == null');
        }
    }
}
